package com.eportal.action.background;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.eportal.action.BaseAction;
import com.eportal.action.background.pojo.Page;
import com.eportal.domain.Sysmenu;
import com.eportal.domain.Sysopt;
import com.eportal.domain.TShelves;
import com.eportal.exception.JdbcException;
import com.eportal.exception.MdKnownRunTimeException;
import com.eportal.exception.NoPermissionException;
import com.eportal.service.SysmenuService;
import com.eportal.service.TShelvesService;
import com.eportal.util.BeanMapUtils;
import com.eportal.util.UploadUtil;
import com.eportal.util.ValidateUtil;
import com.eportal.util.WebUtil;
import com.opensymphony.xwork2.ModelDriven;

import net.sf.json.JSONArray;

/**
 *  开门惊喜管理
 */
public class TShelvesAction extends BaseAction implements ModelDriven<TShelves> {

	private static Logger log = Logger.getLogger(TShelvesAction.class);
	private Map<String, Object> result = new HashMap<String, Object>();
	@Autowired
	private TShelvesService tShelvesService;
	@Autowired
	private SysmenuService resourceService;
	private static String LISTVIEWURL="/admin/tshelves_listView.action";
	
	/**
	 * 列表显示
	 *
	 * @return
	 */

	public String listView() {
		try{
			Map<String,String> params = new HashMap<String,String>();
			String flag = getRequest().getParameter("flag");
			if (StringUtils.isBlank(flag)) {
				flag = "2";
			}
			Sysopt  user= (Sysopt)getSession().getAttribute("user");
			int userRole =  (Integer) getSession().getAttribute("userRole");
			if (userRole == 5 || userRole == 6) {
				return "error";
			}
			StringBuffer where = new StringBuffer(" where 1=1");
			/*if("0".equals(flag)){
				where.append(" !=");
				where.append(1);
				if (userRole == 1 || userRole == 2) {
					where.append(" and (t.ifShare='1' or t.dType='1') ");
				}
				if (userRole == 21) {
					where.append(" and (t.ifShare='1' or a.sysoptId=");
					where.append(user.getSid());
					where.append(")");
				}
				if (userRole == 3 || userRole == 4) {
					where.append(" and (t.ifShare='1' or t.agtCode='");
					where.append(user.getAgtCode());
					where.append("')");
				}
			}else{
				where.append(" =");
				where.append(1);
				if (userRole == 3 || userRole == 4) {
		    		where.append(" and t.agtCode='");
		    		where.append(user.getAgtCode());
		    		where.append("'");
				}
				if (userRole == 21) {
					where.append(" and a.sysoptId=");
					where.append(user.getSid());
				}
			}*/
			
			if("2".equals(flag)){
				where.append(" and (t.isOnLine='1' or t.isOnLine='2')");
				where.append(" and (t.ifShare='1' and dtype='1') ");
			}else if("3".equals(flag)){
				if (userRole == 1 || userRole == 2) {
					
				}
				if (userRole == 21) {
					where.append(" and (a.sysoptId=");
					where.append(user.getSid());
					where.append(")");
				}
				if (userRole == 3 || userRole == 4) {
					where.append(" and (t.agtCode='");
					where.append(user.getAgtCode());
					where.append("')");
				}
			}
	    	
			Page page = new Page();
			String pageNo = getRequest().getParameter("page");
			if (pageNo != null && pageNo.trim().length() > 0) {
				page.setCurrentPage(Integer.parseInt(pageNo));
			}
			
			page = tShelvesService.find(where.toString(), page);
			List<Map<String, Object>> buttonMap = resourceService.getButton(LISTVIEWURL);
			getRequest().setAttribute("buttonMap", buttonMap);
			JSONArray json = JSONArray.fromObject(buttonMap);
			getRequest().setAttribute("buttonJson", json.toString());
			getRequest().setAttribute("page", page);
			getRequest().setAttribute("flag", flag);
			getRequest().setAttribute("userRole", userRole);
			getRequest().setAttribute("agt", user.getAgtCode());
		}catch(Exception e){
			log.error(this, e);
		}
		
		return SUCCESS;
	}

	
	/**
	 * 资源添加页面
	 *
	 * @return
	 */
	public String addView() {
		return SUCCESS;
	}

	/**
	 * 资源添加
	 *
	 * @return
	 */
	public void add() throws IOException {
		Map<String, Object> map = new HashMap<String, Object>();
		String msg = ValidateUtil.validate(tShelves);
		if(tShelves.getName().length()>10){
			msg="名称字数不能超过10字";
		}
		try {
			if (msg.length() == 0) {
					TShelves tShelves = getModel();
					File file = getUpload();
			    	String fileName = getUploadFileName();
			    	result = tShelvesService.addMethod(tShelves,file,fileName);
			} else {
				result.put("code", 0);
				result.put("msg", msg);
			}
		} catch (MdKnownRunTimeException e) {
			log.error("资源添加失败",e);
			 result.put("code", 0);
           result.put("msg",e.getMessage());
		}catch (Exception e) {
			log.error("资源添加失败",e);
			 result.put("code", 0);
           result.put("msg", "操作失败");
		}
		responseBody(result);
	}

	/**
	 * 修改页面
	 *
	 * @return
	 */

	public String editView() {
		String sid = getRequest().getParameter("sid");
		if (StringUtils.isBlank(sid)) {
			return "error";
		}
		Map<String, String>  tshelves = tShelvesService.getBySql(Long.parseLong(sid));
		getRequest().setAttribute("tshelves", tshelves);
	return SUCCESS;
	}

	/**
	 * 修改
	 *
	 * @return
	 */
	public void edit() throws IOException {
		String msg = ValidateUtil.validate(tShelves);
		if(tShelves.getName().length()>10){
			msg="名称字数不能超过10字";
		}
		try {
			if (msg.length() == 0) {
				TShelves tShelves = getModel();
				File file = getUpload();
		    	String fileName = getUploadFileName();
		    	result = tShelvesService.updateMethod(tShelves,file,fileName);
			} else {
				result.put("code", -1);
				result.put("msg", msg);
			}
		}catch (MdKnownRunTimeException e) {
			log.error("资源修改失败",e); 
			result.put("code", 0);
            result.put("msg",e.getMessage());
		}catch (Exception e) {
			log.error("资源修改失败",e);  
			result.put("code", 0);
            result.put("msg", "操作失败");
		}
		responseBody(result);
	}

	
	@Override
	public TShelves getModel() {
		return tShelves;
	}

	private TShelves tShelves = new TShelves();
}
